// import embedpano from '../../public/krpano_static/krpano.js'
import { 
    ElementType,
    BaseElement
    } from './Types'
export default class KrpanoUtil {

    /**
     * 
     * @param bean 
    initLrpano(bean : KrpanoInitBean){
		// eslint-disable-next-line no-undef
        window.embedpano(bean)
    }
     */

    private readonly krpanoInstance: any;

    constructor(krpanoInstance:any){
        this.krpanoInstance = krpanoInstance;
    }

    public insertElement(type:ElementType,element:BaseElement):void{
        this.call(`add${type}(${element.name})`);
        this.setElement(type,element)
    }

    public setElement(type:ElementType,element:BaseElement):void{
        const krpano = this.krpanoInstance;
        const call = (str:string):void =>{
            console.log(str)
            krpano.call(str);
        }
        const setProperty = (property:string,value:any):void=>{
            call(`set(${type}[${element.name}].${property},${value})`);
        }
        for (const key in element) {
            const value = element[key];
            setProperty(key,value);
        }
    }

    public removeElement(type:ElementType,name:string):void{
        this.call(`remove${type}(${name});`)
    }

    public getElements<T extends BaseElement>(type:ElementType,name?:string):T[]{
        const krpano = this.krpanoInstance;
        const arr:T[] = krpano.get(type).getArray();
        if(!name){
            return arr;
        }else {
            // 过滤包含name的数组
            return arr.filter((item:BaseElement) => item.name.indexOf(name) > 0); 
        }
    }

    public getElement<T extends BaseElement>(type:ElementType,name:string):T|null{
        const arr:T[] = this.getElements(type,name);
        for(let i in arr){
            if(arr[i].name === name){
                return arr[i];
            }
        }
        return null;
    }

    public callAction(actionName:string,params:string):void{
        console.log('=====callAction====>',actionName,params);
        const str = `${actionName}(${params})`;
        this.call(str);
    }

    public call(str:string):void {
        console.log(str)
        this.krpanoInstance.call(str)
    }
}

